package com.my.oschina.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.my.oschina.MainActivity;
import com.my.oschina.R;
import com.my.oschina.app.AppContext;
import com.my.oschina.ui.LoginActivity;
import com.my.oschina.ui.UserInfoActivity;
import com.slidingmenu.lib.SlidingMenu;

/**
 * 侧边导航
 * 
 * @author XuShaoJie
 * 
 * @version 2013-05-23
 */
public class MenuFragment extends SherlockFragment {

	private MenuLayout layout;
	private int currentIndex = 0;
	public static int LOGIN_REQ_TRADE = 100;
	public static int LOGIN_REQ_ACC = 101;

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		showNews(true);

		drawButtonsBackground(currentIndex);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// 加载侧边导航布局文件
		View view = inflater.inflate(R.layout.slm_left_layout,
				container, false);
		layout = new MenuLayout();
		layout.news = (Button) view.findViewById(R.id.menu_news);
		layout.question = (Button) view.findViewById(R.id.menu_question);
		layout.tweet = (Button) view.findViewById(R.id.menu_tweet);
		layout.user = (Button) view.findViewById(R.id.menu_user);
		layout.more = (Button) view.findViewById(R.id.menu_more);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		layout.news.setOnClickListener(onClickListener);
		layout.question.setOnClickListener(onClickListener);
		layout.tweet.setOnClickListener(onClickListener);
		layout.user.setOnClickListener(onClickListener);
		layout.more.setOnClickListener(onClickListener);
	}

	public int getCurrentIndex() {
		return currentIndex;
	}

	/**
	 * 侧边导航-资讯
	 */
	private boolean showNews(boolean reset) {
		ActionBar actionBar = getSherlockActivity().getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(getString(R.string.menu_news));
		actionBar.setDisplayHomeAsUpEnabled(true);
		if (currentIndex == 0 && !reset) {
			getSlidingMenu().showContent();
			return true;
		}
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		currentIndex = 0;
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		Fragment tf = ((MainActivity) getActivity()).getQuestionFragment();
		Fragment af = ((MainActivity) getActivity()).getTweetFragment();
		ft.hide(tf);
		ft.hide(af);

		InfoFragment fragment = ((MainActivity) getActivity())
				.getInfoFragment();

		if (fragment.isAdded() && fragment.isHidden()) {
			ft.show(fragment);
		} else if (!fragment.isAdded()) {
			ft.add(R.id.content_frame, fragment, InfoFragment.class.getName());
		}
		ft.commit();
		// TODO 待解决：记住上次显示的Tab位置
		// 侧边导航切换时，要在显示内容前，重新设置其ActionBar的Tab
		fragment.initActionBarAndTab(actionBar, R.string.tab_news_info,
				R.string.tab_news_blog, R.string.tab_news_read, 0);

		getSlidingMenu().showContent();

		return false;
	}

	/**
	 * 侧边导航-问答
	 */
	private boolean showQuestion(boolean reset) {
		ActionBar actionBar = getSherlockActivity().getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(getString(R.string.menu_question));
		actionBar.setDisplayHomeAsUpEnabled(true);
		if (currentIndex == 1 && !reset) {
			getSlidingMenu().showContent();
			return true;
		}
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		currentIndex = 1;
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		Fragment tf = ((MainActivity) getActivity()).getInfoFragment();
		Fragment af = ((MainActivity) getActivity()).getTweetFragment();
		ft.hide(tf);
		ft.hide(af);

		QuestionFragment fragment = ((MainActivity) getActivity())
				.getQuestionFragment();

		if (fragment.isAdded() && fragment.isHidden()) {
			ft.show(fragment);
		} else if (!fragment.isAdded()) {
			ft.add(R.id.content_frame, fragment,
					QuestionFragment.class.getName());
		}
		ft.commit();
		// 侧边导航切换时，要在显示内容前，重新设置其ActionBar的Tab
		fragment.initActionBarAndTab(actionBar, R.string.tab_question_ask,
				R.string.tab_question_share, R.string.tab_question_other,
				R.string.tab_question_job, R.string.tab_question_site, 0);

		getSlidingMenu().showContent();
		return false;
	}

	/**
	 * 侧边导航-动弹
	 * 
	 * @param reset
	 * @return
	 */
	private boolean showTweet(boolean reset) {
		ActionBar actionBar = getSherlockActivity().getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(getString(R.string.menu_tweet));
		actionBar.setDisplayHomeAsUpEnabled(true);
		if (currentIndex == 2 && !reset) {
			getSlidingMenu().showContent();
			return true;
		}
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		currentIndex = 2;
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		Fragment tf = ((MainActivity) getActivity()).getInfoFragment();
		Fragment af = ((MainActivity) getActivity()).getQuestionFragment();
		ft.hide(tf);
		ft.hide(af);

		TweetFragment fragment = ((MainActivity) getActivity())
				.getTweetFragment();

		if (fragment.isAdded() && fragment.isHidden()) {
			ft.show(fragment);
		} else if (!fragment.isAdded()) {
			ft.add(R.id.content_frame, fragment, TweetFragment.class.getName());
		}
		ft.commit();
		// 侧边导航切换时，要在显示内容前，重新设置其ActionBar的Tab
		fragment.initActionBarAndTab(actionBar, R.string.tab_tweet_lastest,
				R.string.tab_tweet_hot, R.string.tab_tweet_my, 0);

		getSlidingMenu().showContent();
		return false;
	}

	/**
	 * 侧边导航-我的空间
	 */
	private void showMyActive() {
		AppContext ac = (AppContext) getSherlockActivity().getApplication();
		if (!ac.isLogin()) {
			Intent i = new Intent(getSherlockActivity(), LoginActivity.class);
			startActivity(i);
		} else {
			Intent i = new Intent(getSherlockActivity(), UserInfoActivity.class);
			startActivity(i);
		}
	}

	/**
	 * 侧边导航-更多
	 */
	private void showMore() {
		// Intent i = new Intent(getActivity(), MoreActivity.class);
		// startActivity(i);
	}

	private SlidingMenu getSlidingMenu() {
		return ((MainActivity) getActivity()).getSlidingMenu();
	}

	/**
	 * 侧边导航点击事件监听器
	 */
	private View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.menu_news:
				showNews(false);
				drawButtonsBackground(0);
				break;
			case R.id.menu_question:
				showQuestion(false);
				drawButtonsBackground(1);
				break;
			case R.id.menu_tweet:
				showTweet(false);
				drawButtonsBackground(2);
				break;
			case R.id.menu_user:
				showMyActive();
				drawButtonsBackground(3);
				break;
			case R.id.menu_more:
				showMore();
				drawButtonsBackground(4);
				break;
			}
		}
	};

	/**
	 * 设置侧边导航选中背景
	 * 
	 * @param position
	 */
	private void drawButtonsBackground(int position) {
		layout.news.setBackgroundResource(R.drawable.slm_left_news_bg);
		layout.question.setBackgroundResource(R.drawable.slm_left_question_bg);
		layout.tweet.setBackgroundResource(R.drawable.slm_left_tweet_bg);
		layout.user.setBackgroundResource(R.drawable.slm_left_user_bg);
		layout.more.setBackgroundResource(R.drawable.slm_left_more_bg);

		switch (position) {
		case 0:
			layout.news.setBackgroundResource(R.color.slm_left_news);
			break;
		case 1:
			layout.question.setBackgroundResource(R.color.slm_left_question);
			break;
		case 2:
			layout.tweet.setBackgroundResource(R.color.slm_left_tweet);
			break;
		case 3:
			layout.user.setBackgroundResource(R.color.slm_left_user);
			break;
		case 4:
			layout.more.setBackgroundResource(R.color.slm_left_more);
			break;
		}
	}

	private class MenuLayout {
		Button news;
		Button question;
		Button tweet;
		Button user;
		Button more;
	}

}
